/**
 * 操作日志模块 - Mock API
 * @description 提供操作日志相关的Mock接口
 */

import Mock from 'mockjs'
import { createResponse, createPageResponse } from './index'
import logger from '@/utils/logger'

/**
 * 设置操作日志Mock接口
 */
export const setupOperationLogsMock = (): void => {
  logger.info('[MOCK] 注册操作日志接口...')

  // 1. 获取操作日志列表
  Mock.mock(/\/api\/v1\/operation-logs(\?.*)?/, 'get', (options) => {
    try {
      logger.debug('[MOCK] 操作日志列表请求:', options.url)
      
      const urlParams = new URLSearchParams(options.url.split('?')[1] || '')
      const page = parseInt(urlParams.get('page') || '1')
      const pageSize = parseInt(urlParams.get('pageSize') || '20')
      
      // 生成模拟操作日志数据
      const logs = Array.from({ length: Math.min(pageSize, 50) }, (_, index) => ({
        id: `LOG${Date.now()}${index}`,
        operationType: Mock.Random.pick(['CREATE', 'UPDATE', 'DELETE', 'VIEW']),
        tableName: Mock.Random.pick(['customers', 'sales', 'products']),
        recordId: Mock.Random.integer(1, 1000),
        fieldName: Mock.Random.pick(['customerName', 'status', 'phone']),
        oldValue: Mock.Random.ctitle(2, 4),
        newValue: Mock.Random.ctitle(2, 4),
        operatorId: Mock.Random.integer(1, 10),
        operatorName: Mock.Random.cname(),
        operationReason: Mock.Random.cparagraph(1, 2),
        createdAt: Mock.Random.datetime(),
        ipAddress: Mock.Random.ip(),
        userAgent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)'
      }))

      const total = Mock.Random.integer(100, 500)
      
      return createPageResponse(logs, total, page, pageSize)
    } catch (error) {
      logger.error('[MOCK] 操作日志列表接口错误:', error)
      return createResponse(null, '获取操作日志失败', 500)
    }
  })

  // 2. 获取操作日志详情
  Mock.mock(/\/api\/v1\/operation-logs\/([^\/\?]+)(\?.*)?$/, 'get', (options) => {
    try {
      const urlParts = options.url.split('/')
      const logId = urlParts[urlParts.length - 1].split('?')[0]
      logger.debug('[MOCK] 操作日志详情请求:', logId)
      
      const logDetail = {
        id: logId,
        operationType: 'UPDATE',
        tableName: 'customers',
        recordId: 123,
        fieldName: 'customerName',
        oldValue: '张三',
        newValue: '张三丰',
        operatorId: 1,
        operatorName: '管理员',
        operationReason: '客户信息更正',
        createdAt: new Date().toISOString(),
        ipAddress: '192.168.1.100',
        userAgent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)',
        additionalInfo: {
          module: '客户管理',
          action: '编辑客户',
          changes: ['姓名从"张三"修改为"张三丰"']
        }
      }
      
      return createResponse(logDetail, '获取操作日志详情成功')
    } catch (error) {
      logger.error('[MOCK] 操作日志详情接口错误:', error)
      return createResponse(null, '获取操作日志详情失败', 500)
    }
  })

  // 3. 获取操作日志统计
  Mock.mock('/api/v1/operation-logs/statistics', 'get', () => {
    try {
      const statistics = {
        total: Mock.Random.integer(1000, 5000),
        today: Mock.Random.integer(50, 200),
        thisWeek: Mock.Random.integer(300, 800),
        thisMonth: Mock.Random.integer(1000, 3000),
        byType: {
          CREATE: Mock.Random.integer(100, 500),
          UPDATE: Mock.Random.integer(200, 800),
          DELETE: Mock.Random.integer(10, 100),
          VIEW: Mock.Random.integer(500, 2000)
        },
        byOperator: Array.from({ length: 5 }, () => ({
          operatorName: Mock.Random.cname(),
          count: Mock.Random.integer(50, 300)
        }))
      }
      
      return createResponse(statistics, '获取操作日志统计成功')
    } catch (error) {
      logger.error('[MOCK] 操作日志统计错误:', error)
      return createResponse(null, '获取操作日志统计失败', 500)
    }
  })

  logger.info('[MOCK] 操作日志接口注册完成')
}